//
//  DateToolClass.swift
//  FKTbroker
//
//  Created by daojial on 2019/11/28.
//  Copyright © 2019 RRJ. All rights reserved.
//

import UIKit

/**时间格式*/
enum FormatState {
    case FormatStateYMD //"yyyy-MM-dd"
    case FormatStateYMD_Hmd //"yyyy-MM-dd HH:mm:ss"
    case FormatStateHm //"HH:mm"
}
class DateToolClass: NSObject {
   
 /**得到当前时间DateStr*/
  class func getCurrentDate(state:FormatState) -> String {
        let formatStr = self.getFormatWithState(state: state)
    
        let formatter = DateFormatter.init()
            formatter.dateFormat = formatStr
        
        let dateTime = formatter.string(from: NSDate.init() as Date)
      
        return dateTime
    }
    
    
    //获取类型
    private class func getFormatWithState(state:FormatState) -> String {
        var formatStr = String()
        
        switch state {
        case .FormatStateYMD:
            formatStr = "yyyy-MM-dd"
        case .FormatStateHm:
            formatStr = "HH:mm"
        case .FormatStateYMD_Hmd:
            formatStr = "yyyy-MM-dd HH:mm:ss"
        }
        
        return formatStr
    }
    
   /**字符串转Date*/
    private class func dateFromDayStr(dateDay:String) -> Date {
        let FormaStr = DateFormatter.init()
        FormaStr.dateFormat = "yyyy-MM-dd"
//        FormaStr.timeZone = TimeZone.init(abbreviation: "UTC")
        let otherDate = FormaStr.date(from: dateDay)
        return otherDate!
    }
   
    
  /**星期几*/
  class func getWeekDayWithDate(dateStr:String) ->String

  {
//  let dateArr = dateStr.componentsSeparatedByString("-")
    let dateArr = dateStr.components(separatedBy: "-")
    
              if dateArr.count == 3

              {

              var y = Int(dateArr[0])!

              var m = Int(dateArr[1])!

              let d = Int(dateArr[2])!

              if m == 1 || m == 2

              {

              m += 12

              y -= 1

              }

              let iWeek = (d+2*m+3*(m+1)/5+y+y/4-y/100+y/400)%7

              switch iWeek

              {

              case 0: return "星期一"

              case 1: return "星期二"

              case 2: return "星期三"

              case 3: return "星期四"

              case 4: return "星期五"

              case 5: return "星期六"

              case 6: return "星期天"

              default:
              return ""
              }
                
              }else{

              return "星期未知"
              }
  }

    
    
 /*dateType:任意类型
  *日期时间对比
  */
  class  func compareDate(oneDate:AnyObject,towDate:AnyObject,state:FormatState) -> Int {
        
    let formatStr = self.getFormatWithState(state: state)
      
        
        let dateFormatter = DateFormatter.init()
        dateFormatter.date(from: formatStr)
        
        var oneDateStr = String.init()
        var towDateStr = String.init()
        
        if oneDate is NSDate {
            oneDateStr = dateFormatter.string(from: oneDate as! Date)
        } else {
            oneDateStr = oneDate as! String
        }
        if towDate is NSDate {
            towDateStr = dateFormatter.string(from: towDate as! Date)
        } else {
            towDateStr = towDate as! String
        }
        
        
        let dateA = dateFormatter.date(from: oneDateStr)
        let dateB = dateFormatter.date(from: towDateStr)
        
        let result = dateA?.compare(dateB!)
        
        //dateA < dateB
        if result == .orderedAscending{//-1
           return -1
        }
        //dateA > dateB
        if result == .orderedDescending {//
          return 1
        }
        return 0
    }

    
    /**获取指定指定日期  n天前后日期
       *n (负前正后)
     */
    class func getDateNDay(dateDay:String,n:Int,state:FormatState) -> String {
        let oneDate = self.dateFromDayStr(dateDay: dateDay)
        
        let oneday:TimeInterval = TimeInterval(24*60*60*1)
        
        let otherDate = oneDate.addingTimeInterval(oneday*Double(n))
            
        let dateFormat = DateFormatter.init()
        dateFormat.dateFormat = self.getFormatWithState(state: state)
        
        return dateFormat.string(from: otherDate)
    }
    
    
}
